<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

include_once('../arc2-starter-pack/config.php');
$store = ARC2::getStore($arc_config);

$output = array();

$q = "
prefix dct:<http://purl.org/dc/terms/>
prefix xsd:<http://www.w3.org/2001/XMLSchema#>
select distinct ?paper ?year where {
	?paper dct:creator <".$_GET["uri"]."> .
	?paper dct:date ?year
} order by ?year";

$rows = $store->query($q, 'rows');
$years = array();
foreach ($rows as $row) {
	$date = $row["year"];
	preg_match("/[0-9][0-9][0-9][0-9]/", $date, $matches);
	foreach ($matches as $d) {
		if (isset($years[$d])) {
			$years[$d]++;
		}
		else {
			$years[$d] = 1;
		}	
	}
}

foreach ($years as $y => $c) {
	$year = array();
	$year["year"] = $y;
	$year["count"] = $c;
	$output[] = $year;
}
#sort the dates in ascending order
usort($output, function($a, $b) { return $a["year"]-$b["year"]; });

# output as json
header('Content-type: text/plain');
echo json_encode($output);

?>
